import { isFunction } from './typeUtil'

export function isCollectionEmpty (collection) {
  return !collection || (!collection.length && !collection.size)
}

export function isNotEmpty (collection) {
  return !isCollectionEmpty(collection)
}

export function collectionSum (collection, summer = b => b) {
  return collection.reduce((a, b) => summer(b) + a, 0)
}

export function removeCollectionItem (collection, valueOrFunc) {
  if (!isFunction(valueOrFunc)) {
    valueOrFunc = (el) => {
      return el === valueOrFunc
    }
  }
  const idx = collection.findIndex(el => valueOrFunc(el))
  if (idx > -1) {
    collection.splice(idx, 1)
  }
}
